package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.alibaba.fastjson.JSONObject;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.base.util.LocalAttrUtils;
import com.ztesoft.bss.common.util.ContextUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:AZ端营业区特殊处理
 * File Name:AZBusinessAreaAttrHandler
 * Modification History:
 * Date             Author      Version     Description
 * ------------------------------------------------------------------
 * 2021-6-4 17:00   邱萌         1.0       1.0 Version
 **/
public class AZBusinessAreaAttrHandler extends AbstractAttrHandler {

    @Override
    public void after(KiteAttr attr, KiteInst inst, AttrData data) {
        // 浙江 mv、sdh、光纤、以太网 AZ端营业区必填
//
        String provinceCode = ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr();
        String cityCode = ContextUtil.getLoginInfo().getUserInfo().getPostLanId();
        if (LocalKeyConsts.PROVINCE_ZHEJIANG.equals(provinceCode)) {
            attr.setAttrValueTypeId("98A");
            //如果当前组织是网格，则查询组织信息，从组织信息中获取县分填充
            JSONObject result= LocalAttrUtils.getInstance().queryOrgInfo(null,provinceCode,cityCode,LocalKeyConsts.X40002T);
            if(null!=result.getJSONArray("LIST")&&result.getJSONArray("LIST").size()>0) {
                List<JSONObject> orgInfoList = result.getJSONArray("LIST").toJavaList(JSONObject.class);
                List<KiteAttrValue> attrValues = new ArrayList<>();
                attrValues.add(LocalAttrUtils.pleaseSelect());
                for (JSONObject orgInfo : orgInfoList) {
                    KiteAttrValue kiteAttrValue = new KiteAttrValue();
                    kiteAttrValue.setAttrValue(orgInfo.getString("NAME"));
                    kiteAttrValue.setAttrValueName(orgInfo.getString("NAME"));
                    kiteAttrValue.setAttrValueDesc(orgInfo.getString("ORG_CODE"));
                    kiteAttrValue.setAttrId(attr.getAttrId());
                    kiteAttrValue.setSortBy("1");
                    attrValues.add(kiteAttrValue);
                }
                attr.setAttrValues(attrValues);
            }
            attr.setIsNull(KeyConsts.IFTRUE_F);

        }

        super.after(attr, inst, data);
    }
}
